using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VKSGUT
    {
        public DataRow Convert2KSGUT(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryKSGUT(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["KSGMANDANT"] = " ".PadRight(2 ); 
         dwReturn["KSGAUFKRS"] = " ".PadRight(2 ); 
         dwReturn["KSGAUFLFD"] = " ".PadRight(6 ); 
         dwReturn["KSGAUFPOS"] = " ".PadRight(3 ); 
         dwReturn["KSGAFOLGE"] = " ".PadRight(0 ); 
         dwReturn["KSGAENDZL"] = " ".PadRight(0 ); 
         dwReturn["KSGSA"] = " ".PadRight(1 ); 
         dwReturn["KSGKOGRP"] = " ".PadRight(4 ); 
         dwReturn["KSGSVERB"] = " ".PadRight(0 ); 
         dwReturn["KSGSKOST"] = " ".PadRight(0 ); 
         dwReturn["KSGSAUFT01"] = " ".PadRight(0 ); 
         dwReturn["KSGSAUFT02"] = " ".PadRight(0 ); 
         dwReturn["KSGSAUFT03"] = " ".PadRight(0 ); 
         dwReturn["KSGSAUFT04"] = " ".PadRight(0 ); 
         dwReturn["KSGSAUFT05"] = " ".PadRight(0 ); 
         dwReturn["KSGSGKSTKRS"] = " ".PadRight(2 ); 
         dwReturn["KSGSGKST"] = " ".PadRight(4 ); 
         dwReturn["KSGIVERB"] = " ".PadRight(0 ); 
         dwReturn["KSGIKOST"] = " ".PadRight(0 ); 
         dwReturn["KSGIAUFT01"] = " ".PadRight(0 ); 
         dwReturn["KSGIAUFT02"] = " ".PadRight(0 ); 
         dwReturn["KSGIAUFT03"] = " ".PadRight(0 ); 
         dwReturn["KSGIAUFT04"] = " ".PadRight(0 ); 
         dwReturn["KSGIAUFT05"] = " ".PadRight(0 ); 
         dwReturn["KSGIGKSTKRS"] = " ".PadRight(2 ); 
         dwReturn["KSGIGKST"] = " ".PadRight(4 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapKSGUT(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveKSGUT(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryKSGUT(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapKSGUT(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertKSGUT(dwLocal, ITXDBVERSION);
            }
            UpdateKSGUT(dwLocal);
        }
        public void InsertKSGUT(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO KSGUT(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryKSGUT(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM KSGUT WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM KSGUT  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "KSGUT";
            Return = Result;
            return Return;
        }
        public void DeleteKSGUT(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM KSGUT   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateKSGUT(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE KSGUT SET "
                    +"KSGMANDANT= :KSGMANDANT,"
                    +"KSGAUFKRS= :KSGAUFKRS,"
                    +"KSGAUFLFD= :KSGAUFLFD,"
                    +"KSGAUFPOS= :KSGAUFPOS,"
                    +"KSGAFOLGE= :KSGAFOLGE,"
                    +"KSGAENDZL= :KSGAENDZL,"
                    +"KSGSA= :KSGSA,"
                    +"KSGKOGRP= :KSGKOGRP,"
                    +"KSGSVERB= :KSGSVERB,"
                    +"KSGSKOST= :KSGSKOST,"
                    +"KSGSAUFT01= :KSGSAUFT01,"
                    +"KSGSAUFT02= :KSGSAUFT02,"
                    +"KSGSAUFT03= :KSGSAUFT03,"
                    +"KSGSAUFT04= :KSGSAUFT04,"
                    +"KSGSAUFT05= :KSGSAUFT05,"
                    +"KSGSGKSTKRS= :KSGSGKSTKRS,"
                    +"KSGSGKST= :KSGSGKST,"
                    +"KSGIVERB= :KSGIVERB,"
                    +"KSGIKOST= :KSGIKOST,"
                    +"KSGIAUFT01= :KSGIAUFT01,"
                    +"KSGIAUFT02= :KSGIAUFT02,"
                    +"KSGIAUFT03= :KSGIAUFT03,"
                    +"KSGIAUFT04= :KSGIAUFT04,"
                    +"KSGIAUFT05= :KSGIAUFT05,"
                    +"KSGIGKSTKRS= :KSGIGKSTKRS,"
                    +"KSGIGKST= :KSGIGKST,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("KSGMANDANT", OracleType.NVarChar).Value = dwData["KSGMANDANT"].ToString(); 
          or1.Parameters.Add("KSGAUFKRS", OracleType.NVarChar).Value = dwData["KSGAUFKRS"].ToString(); 
          or1.Parameters.Add("KSGAUFLFD", OracleType.NVarChar).Value = dwData["KSGAUFLFD"].ToString(); 
          or1.Parameters.Add("KSGAUFPOS", OracleType.NVarChar).Value = dwData["KSGAUFPOS"].ToString(); 
          or1.Parameters.Add("KSGAFOLGE", OracleType.NVarChar).Value = dwData["KSGAFOLGE"].ToString(); 
          or1.Parameters.Add("KSGAENDZL", OracleType.NVarChar).Value = dwData["KSGAENDZL"].ToString(); 
          or1.Parameters.Add("KSGSA", OracleType.NVarChar).Value = dwData["KSGSA"].ToString(); 
          or1.Parameters.Add("KSGKOGRP", OracleType.NVarChar).Value = dwData["KSGKOGRP"].ToString(); 
          or1.Parameters.Add("KSGSVERB", OracleType.NVarChar).Value = dwData["KSGSVERB"].ToString(); 
          or1.Parameters.Add("KSGSKOST", OracleType.NVarChar).Value = dwData["KSGSKOST"].ToString(); 
          or1.Parameters.Add("KSGSAUFT01", OracleType.NVarChar).Value = dwData["KSGSAUFT01"].ToString(); 
          or1.Parameters.Add("KSGSAUFT02", OracleType.NVarChar).Value = dwData["KSGSAUFT02"].ToString(); 
          or1.Parameters.Add("KSGSAUFT03", OracleType.NVarChar).Value = dwData["KSGSAUFT03"].ToString(); 
          or1.Parameters.Add("KSGSAUFT04", OracleType.NVarChar).Value = dwData["KSGSAUFT04"].ToString(); 
          or1.Parameters.Add("KSGSAUFT05", OracleType.NVarChar).Value = dwData["KSGSAUFT05"].ToString(); 
          or1.Parameters.Add("KSGSGKSTKRS", OracleType.NVarChar).Value = dwData["KSGSGKSTKRS"].ToString(); 
          or1.Parameters.Add("KSGSGKST", OracleType.NVarChar).Value = dwData["KSGSGKST"].ToString(); 
          or1.Parameters.Add("KSGIVERB", OracleType.NVarChar).Value = dwData["KSGIVERB"].ToString(); 
          or1.Parameters.Add("KSGIKOST", OracleType.NVarChar).Value = dwData["KSGIKOST"].ToString(); 
          or1.Parameters.Add("KSGIAUFT01", OracleType.NVarChar).Value = dwData["KSGIAUFT01"].ToString(); 
          or1.Parameters.Add("KSGIAUFT02", OracleType.NVarChar).Value = dwData["KSGIAUFT02"].ToString(); 
          or1.Parameters.Add("KSGIAUFT03", OracleType.NVarChar).Value = dwData["KSGIAUFT03"].ToString(); 
          or1.Parameters.Add("KSGIAUFT04", OracleType.NVarChar).Value = dwData["KSGIAUFT04"].ToString(); 
          or1.Parameters.Add("KSGIAUFT05", OracleType.NVarChar).Value = dwData["KSGIAUFT05"].ToString(); 
          or1.Parameters.Add("KSGIGKSTKRS", OracleType.NVarChar).Value = dwData["KSGIGKSTKRS"].ToString(); 
          or1.Parameters.Add("KSGIGKST", OracleType.NVarChar).Value = dwData["KSGIGKST"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
